﻿package com.yunhuakeji.wddx.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.yunhuakeji.wddx.dao.ITicketed;
import com.yunhuakeji.wddx.utils.CommandType;
import com.yunhuakeji.wddx.utils.ConfigReader;
import com.yunhuakeji.wddx.utils.DataTable;
import com.yunhuakeji.wddx.utils.Datehelper;
import com.yunhuakeji.wddx.utils.EnumTool;
import com.yunhuakeji.wddx.utils.EnumTool.ErrorCode;
import com.yunhuakeji.wddx.utils.OracleConnection;
import com.yunhuakeji.wddx.utils.OracleHelper;
import com.yunhuakeji.wddx.utils.Parameter;
import com.yunhuakeji.wddx.utils.SecurityEncode;
import com.yunhuakeji.wddx.utils.errorHelper;

public class TicketService implements ITicketed {

	
	@Override
	public boolean CheckTicke(String userid, String ticket) {
		if(userid==null||ticket==null||userid.isEmpty()||ticket.isEmpty())return false;
		return com.yunhuakeji.wddx.utils.SecurityEncode.StringEncode(userid+GetUserPassword(userid)).equals(ticket.toLowerCase());
	}

	@Override
	public boolean CheckTicke(String userid, String function_id, String function_name,String ticket) {
		if(userid==null||ticket==null||function_id==null||userid.isEmpty()||ticket.isEmpty()||function_id.isEmpty())return false;
		if(!ConfigReader.GetValueForJZHFW(function_name).equals(function_id))return false;
		String sql="select getmd5(?||username||password||(select b.integrate_key from client_function a join xtgl_integratekey b on a.integrate_id=b.integrate_id where a.function_id=?)) ticket from xtgl_userlogininfo where username=?";
		try {
			Parameter [] p = {new Parameter("fjzf", 0, EnumTool._验证模块.附加字符串.getCode()),new Parameter("function_id", 0, function_id),new Parameter("username", 0, userid)};
			DataTable dt = OracleHelper.ExecuteDataSet(new OracleConnection(EnumTool._数据库连接字符串.本地连接字符串.getCode()), CommandType.Text, sql ,p).Tables[0];
			if(dt.Rows.length>0)
				return dt.Rows[0].Columns[0].colValue.equals(ticket.toUpperCase());
			else return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean CheckTicke(String userid, String authTime,String function_name,String ticket,String notuse) {
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			if(!Datehelper.sub(Calendar.getInstance().getTime(), df.parse(authTime), 30))
				return false;
		} catch (Exception e) {
			return false;
		}
		return SecurityEncode.StringEncode(userid+authTime+GetFunctionKey(ConfigReader.GetValueForJZHFW(function_name))).toUpperCase().equals(ticket.toUpperCase());
	}
	
	private String GetUserPassword(String userid)
	{
		String sql ="select getpasswordmd5(password) from xtgl_userlogininfo where username=?";
		try {
			DataTable dt= OracleHelper.ExecuteDataSet(new OracleConnection(EnumTool._数据库连接字符串.本地连接字符串.getCode()), CommandType.Text, sql ,new Parameter("username",0 , userid)).Tables[0];
			if(dt.Rows.length>0)
			{
				return dt.Rows[0].Columns[0].colValue.toString();
			}
			else return "";
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	private String GetFunctionKey(String function_id)
	{
		String sql="select t2.integrate_key from client_function t1,xtgl_integratekey t2 where t1.integrate_id=t2.integrate_id and t1.function_id=?";
		try {
			DataTable dt= OracleHelper.ExecuteDataSet(new OracleConnection(EnumTool._数据库连接字符串.本地连接字符串.getCode()), CommandType.Text, sql ,new Parameter("Navigation_ID",0 , function_id)).Tables[0];
			if(dt.Rows.length>0)
			{
				return dt.Rows[0].Columns[0].colValue.toString();
			}
			else return "";
		} catch (Exception e) {

			e.printStackTrace();
			return "";
		}
	}

	public String GetFunctionName(String function_id)
	{
		String sql ="select function_name from client_function where function_id=?";
		try {
			DataTable dt= OracleHelper.ExecuteDataSet(new OracleConnection(EnumTool._数据库连接字符串.本地连接字符串.getCode()), CommandType.Text, sql,new Parameter("function_id",0,function_id)).Tables[0];
			if(dt.Rows.length>0)
				return dt.Rows[0].Columns[0].colValue.toString();
			else return errorHelper.GetErrorJson(ErrorCode.模块正在建设中.getCode());
		} catch (Exception e) {
			return errorHelper.GetErrorJson(ErrorCode.数据库链接错误.getCode());
		}
	}
	
}
